﻿using MiniFox.Infrastructure;
using MiniFox.Resources;
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Platform.Core.Models
{
    [JsonObject]
    [Table("widget")]
    public class Widget
    {
        [Key]
        [Display(Name = "名称", ShortName = "名称", Order = 2)]
        [Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired")]
        [JsonProperty("name")]
        [Column("name")]
        public string Name { get; set; }

        [Display(Name = "标题", ShortName = "标题", Order = 3)]
        [Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired")]
        [JsonProperty("title")]
        [Column("title")]
        public string Title { get; set; }

        [Display(Name = "发布者", ShortName = "发布者", Order = 4)]
        [JsonProperty("publisher")]
        [Column("publisher")]
        public string Publisher { get; set; }

        [Display(Name = "发布时间", ShortName = "发布时间", Order = 5)]
        [JsonProperty("publishTime")]
        [Column("publishTime")]
        public DateTime PublishTime { get; set; }

        [Display(Name = "图标", ShortName = "图标", Order = 6)]
        [DataType("icon")]
        [Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired")]
        [JsonProperty("icon")]
        [Column("icon")]
        public string Icon { get; set; }

        [Display(Name = "规格(R x C)", ShortName = "规格(R x C)", Order = 7)]
        [JsonProperty("size")]
        [Column("size")]
        public string Size
        {
            get
            {
                return this.RowSpan + " x " + this.ColSpan;
            }
        }

        [Display(Name = "描述", ShortName = "描述", Order = 12)]
        [JsonProperty("description")]
        [Column("description")]
        public string Description { get; set; }

        [Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired")]
        [JsonProperty("colSpan")]
        [Column("colSpan")]
        public int ColSpan { get; set; }

        [Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired")]
        [JsonProperty("rowSpan")]
        [Column("rowSpan")]
        public int RowSpan { get; set; }

        [Display(Name = "显示标头", ShortName = "显示标头", Order = 8)]
        [JsonProperty("showHeader")]
        [Column("showHeader")]
        public bool ShowHeader { get; set; }

        [Display(Name = "显示底部", ShortName = "显示底部", Order = 9)]
        [JsonProperty("showFooter")]
        [Column("showFooter")]
        public bool ShowFooter { get; set; }

        [Display(Name = "样式", ShortName = "样式", Order = 10)]
        [JsonProperty("className")]
        [Column("className")]
        public string ClassName { get; set; }

        [JsonProperty("schedule")]
        [Column("schedule")]
        public Schedule Schedule { get; set; }

        [JsonProperty("dataSource")]
        public Service DataSource { get; set; }

        [JsonProperty("template")]
        [Column("template")]
        public string Template { get; set; }

        [JsonProperty("script")]
        [Column("script")]
        public string Script { get; set; }

        [Display(Name = "入口地址", ShortName = "入口地址", Order = 11)]
        [JsonProperty("enterUrl")]
        [Column("enterUrl")]
        public string EnterUrl { get; set; }

    }

    [JsonObject]
    public class Service
    {
        [JsonProperty("params")]
        public object Params { get; set; }

        [JsonProperty("headers")]
        public object Headers { get; set; }

        [JsonProperty("data")]
        public object Data { get; set; }

        [JsonProperty("method")]
        public string Method { get; set; }

        [JsonProperty("url")]
        public string Url { get; set; }
    }

    [JsonObject]
    public class Schedule
    {
        [JsonProperty("delay")]
        public int Delay { get; set; }

        [JsonProperty("interval")]
        public int Interval { get; set; }

        [JsonProperty("once")]
        public bool Once
        {
            get
            {
                return this.Interval <= 0;
            }
        }
    }

}
